const api = require('../../utils/api.js');
const app = getApp();

Page({
  data: {
    markers: [
      {
        iconPath: app.imgUrl + 'icon-peisong.png',
        id: 0,
        title: '配送员',
        width: 30,
        height: 40,
      },
      {
        iconPath: app.imgUrl + 'icon-destination.png',
        id: 1,
        title: '目的地',
        width: 30,
        height: 40,
      },
    ],
    points: [],
  },

  onLoad(options) {
    this.setData({
      options: options || {},
    });
    let { id, type } = this.data.options;
    this.loadOrderInfo(id, type);
  },

  onReady() {
    this.mapCtx = wx.createMapContext('map');
  },

  onPullDownRefresh() {
    let { id, type } = this.data.options;
    this.loadOrderInfo(id, type);
  },

  loadOrderInfo(id, _type) {
    let that = this;
    wx.showLoading({
      title: '加载中...',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.order.dispatch,
      data: {
        order_id: id,
        type: _type,
      },
      success: function(res) {
        if (res.code === 0) {
          let { markers, points } = that.data;
          let { latitude, longitude, to_latitude, to_longitude } = res.data;
          let current = {
            latitude,
            longitude,
          };
          let destination = {
            latitude: to_latitude,
            longitude: to_longitude,
          };
          markers[0].latitude = current.latitude;
          markers[0].longitude = current.longitude;
          markers[1].latitude = destination.latitude;
          markers[1].longitude = destination.longitude;
          points = [current, destination];
          that.mapCtx.translateMarker({
            markerId: 0,
            autoRotate: true,
            duration: 1500,
            destination: current,
          });
          that.setData({
            orderInfo: res.data,
            markers,
            points,
          });
        } else {
          wx.showToast({
            title: res.msg || '获取失败',
            icon: 'none',
          });
        }
      },
      complete: function() {
        wx.stopPullDownRefresh();
        wx.hideLoading();
      },
    });
  },

  call(e) {
    let phoneNum = e.currentTarget.dataset.mobile;
    wx.makePhoneCall({
      phoneNumber: phoneNum,
    });
  },
});
